/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ProjectMgtSystem.Bean;

import ProjectMgtSystem.Entities.Task;
import ProjectMgtSystem.Utilities.Conn;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import ProjectMgtSystem.Utilities.*;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author DangDucThinh
 * StartDate    2011-07-21
 * UpdateDate   2011-07-21
 */
public class TaskActionBean extends org.apache.struts.action.ActionForm {
    
    String taskID;
    String taskName;
    String description;

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getTaskID() {
        return taskID;
    }

    public void setTaskID(String taskID) {
        this.taskID = taskID;
    }

    public String getTaskName() {
        return taskName;
    }

    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }

    /**
     *
     */
    public TaskActionBean() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
//        if (getName() == null || getName().length() < 1) {
//            errors.add("name", new ActionMessage("error.name.required"));
//            // TODO: add 'error.name.required' key to your resources
//        }
        return errors;
    }
  
// <editor-fold defaultstate="collapsed" desc="get array list of task">    
    /**
     * get arraylist of task
     */
    public ArrayList<Task> view() throws SQLException {
        ArrayList<Task> taskList = new ArrayList<Task>();
        Conn cnn = new Conn();
        //String sql = "select * from engineer where engineer.EDelete=0";
        String sql = "SELECT * FROM task";
        ResultSet rs = cnn.exec(sql);
        while (rs.next()) {
            Task task = new Task();
            task.setTaskID(rs.getString("TaskID"));
            task.setTaskName(rs.getString("TaskName"));
            task.setDescription(rs.getString("Description"));
            taskList.add(task);
        }
        return taskList;
    }
 // </editor-fold>
 
// <editor-fold defaultstate="collapsed" desc="insert a new task record">      
    /**
     * insert a new task record
     */
    public String insert(String taskID, String taskName, String description) throws SQLException {
         //+++<Hien 07/25/2011: fix code>
        DBHelper.getInstance().initConnection();
        ArrayList<String> arrP= new ArrayList<String>();
        arrP.add(taskID);
        arrP.add(taskName);
        arrP.add(description);        
        String message = DBHelper.getInstance().executeUpdateProcReturn("Insert_Task", arrP);
        DBHelper.getInstance().closeConnection();
        return message;
//        int row = 0;
//        Conn cnn = new Conn();
//        //String sql = "insert into engineer(engineerid, firstname, lastname, major, description, edelete) values(?,?,?,?,?,?)";
//        String sql = "INSERT INTO task(TaskId, TaskName, Description) values(?,?,?)";
//        PreparedStatement prstm = cnn.getPrepareStatement(sql);
//        prstm.setString(1, taskID);
//        prstm.setString(2, taskName);
//        prstm.setString(3, description);
//        row = prstm.executeUpdate();
//        return row;
         //---<Hien 07/25/2011: fix code>
    }
  // </editor-fold>
    
// <editor-fold defaultstate="collapsed" desc="delete a new task record">          
    /**
     * Delete
     */
    public String delete(String taskID) throws SQLException {
         //+++<Hien 07/25/2011: fix code>
        DBHelper.getInstance().initConnection();
        ArrayList<String> arrP= new ArrayList<String>();
        arrP.add(taskID);        
        String message = DBHelper.getInstance().executeUpdateProcReturn("Delete_Task", arrP);
        DBHelper.getInstance().closeConnection();
        return message;
        
//        int row = 0;
//        Conn cnn = new Conn();
//        //String sql = "delete from engineer where engineer.engineerid='" + engineerid + "'";
//        String sql = "DELETE FROM task WHERE task.TaskID='" + taskID + "'";
//        row = cnn.delete(sql);
//        return row;
    }
 // </editor-fold>
    
// <editor-fold defaultstate="collapsed" desc="select">          
    /**
     * Select with @param
     */
    public ArrayList<Task> select(String taskID) throws SQLException {
        ArrayList<Task> taskList = new ArrayList<Task>();
        Conn cnn = new Conn();
        //String sql = "select * from engineer where engineer.engineerid='" + engineerid + "'";
        String sql = "SELECT * FROM task WHERE task.TaskID='" + taskID + "'";
        ResultSet rs = cnn.exec(sql);
        while (rs.next()) {
            Task task = new Task();
            task.setTaskID(rs.getString("TaskID"));
            task.setTaskName(rs.getString("TaskName"));
            task.setDescription(rs.getString("Description"));
            taskList.add(task);
        }
        return taskList;
    }
// </editor-fold>    
    
// <editor-fold defaultstate="collapsed" desc="update a new task record">          
    /**
     * Update
     */
    public int update(String taskID, String taskName, String description) throws SQLException {
        int row = 0;
        Conn cnn = new Conn();
        //String sql = "update engineer e set e.firstname=?, e.lastname=?, e.major=?, e.description=? where e.engineerid='" + engineerid + "'";
        String sql = "UPDATE task t SET t.TaskName=?, t.Description=? WHERE t.TaskID='" + taskID + "'";
        
        PreparedStatement prstm = cnn.getPrepareStatement(sql);
        prstm.setString(1, taskName);
        prstm.setString(2, description);

        row = prstm.executeUpdate();
        return row;
    }
// </editor-fold>    
}
